package com.eds.mall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class MallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        // 配置跨域信息
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedHeader("*"); // 允许所有请求head信息
        config.addAllowedMethod("*"); // 允许所有请求类型
        // springboot 升级 2.4.0以上版本需要进行设置
        config.addAllowedOriginPattern("*");
        config.setAllowCredentials(true);  // 携带认证信息

        // 路径跨域匹配，适合所有路径
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}
